package com.adun.scheduler;

import com.adun.scheduler.controller.SchedulerJobController;
import com.adun.scheduler.controller.SchedulerJobLocalController;
import com.adun.scheduler.local.SchedulerJobLocalRegister;
import com.adun.scheduler.properties.SchedulerJobLocalProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor;

/**
 * @author ZDF
 * @date 2025/8/8
 */
@Configuration
@Slf4j
@AutoConfigureAfter(ConfigurationPropertiesAutoConfiguration.class)
@ConditionalOnBean(ScheduledAnnotationBeanPostProcessor.class)
@ConditionalOnProperty(prefix = SchedulerJobLocalHandlerAutoConfiguration.PREFIX, name = "enable", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(SchedulerJobLocalProperties.class)
public class SchedulerJobLocalHandlerAutoConfiguration {

    public static final String PREFIX = "dynamic.scheduler.local";

    public SchedulerJobLocalHandlerAutoConfiguration() {
        printLogBanner();
        log.info("定时任务本地自动配置启动,可通过{}.enable=false禁用整体", PREFIX);
    }


    private void printLogBanner() {
        String banner = "\n" +
                "___  _   _ _  _ ____ _  _ _ ____    _    ____ ____ ____ _       ____ ____ _  _ ____ ___  _  _ _    ____ ____ \n" +
                "|  \\  \\_/  |\\ | |__| |\\/| | |    __ |    |  | |    |__| |    __ [__  |    |__| |___ |  \\ |  | |    |___ |__/ \n" +
                "|__/   |   | \\| |  | |  | | |___    |___ |__| |___ |  | |___    ___] |___ |  | |___ |__/ |__| |___ |___ |  \\ \n" +
                "                                                                                                             ";
        log.info(banner);
    }

    @Bean
    public SchedulerJobLocalRegister schedulerJobLocalRegister() {
        return new SchedulerJobLocalRegister();
    }

    @Bean
    public SchedulerJobManager schedulerJobManager(){
        return new SchedulerJobManager();
    }

    @Bean
    @ConditionalOnBean(WebMvcAutoConfiguration.class)
    public SchedulerJobController schedulerJobController() {
        return new SchedulerJobController();
    }

    @Bean
    @ConditionalOnBean(WebMvcAutoConfiguration.class)
    public SchedulerJobLocalController schedulerJobLocalController() {
        return new SchedulerJobLocalController();
    }
}
